package com.hyacinth.wapper;

import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RetValue {
	
	private static final Logger logger = LoggerFactory.getLogger(RetValue.class);
	
	private int code;
	private boolean isSuccess=false;
	private String message;
	private Exception exception;
	private Object askData;
	
	
	public Object getAskData() {
		return askData;
	}
	public void setAskData(Object askData) {
		this.askData = askData;
	}
	public RetValue(String message) {
		super();
		this.message = message;
	}
	public RetValue() {
		super();
	}
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public boolean isSuccess() {
		return isSuccess;
	}
	public void setSuccess(boolean isSuccess) {
		this.isSuccess = isSuccess;
	}
	public void setException(Exception exception) {
		this.isSuccess=false;
		this.message="程序异常:"+exception.getMessage();
		this.exception = exception;
	}
	public Exception getException() {
		return exception;
	}
	
	public String getReturnString(){
		return (isSuccess?"OK":"ERROR")+"_"+message;
	}
	
	public String getJsonString(){
		JSONObject obj=new JSONObject();
		obj.put("isSuccess", isSuccess);
		obj.put("status", isSuccess?"OK":"ERROR");
		obj.put("message", message);
		obj.put("exception", exception==null?null:exception.getMessage());
		return obj.toString();
	}
	
	public String getShortException(){
		if(exception!=null){
			return exception.getMessage();
		}else{
			return "";
		}
	}
	
	public static RetValue getExceptionReturnValue(Exception e){
		RetValue rv=new RetValue();
		rv.setException(e);
		return rv;
	}
	
	public static RetValue getSuccessReturnValue(){
		RetValue rv=new RetValue();
		rv.setSuccess(true);
		rv.setMessage("操作成功");
		return rv;
	}
	
	public static RetValue getSuccessReturnValue(String msg){
		RetValue rv=new RetValue();
		rv.setSuccess(true);
		rv.setMessage(msg);
		return rv;
	}
	
	public static RetValue getReturnValue(boolean isSucc,Object obj){
		RetValue rv=new RetValue();
		rv.setSuccess(isSucc);
		rv.setAskData(obj);
		return rv;
	}
	
	public static RetValue getErrorReturnValue(String msg){
		RetValue rv=new RetValue();
		rv.setSuccess(false);
		rv.setMessage(msg);
		return rv;
	}
	
	public static RetValue getReturnValue(Boolean success,String message){
		RetValue rv=new RetValue();
		rv.setSuccess(success);
		rv.setMessage(message);
		return rv;
	}
	
	public static RetValue getReturnValue(Boolean success,String message, Object obj){
		RetValue rv=new RetValue();
		rv.setSuccess(success);
		rv.setMessage(message);
		rv.setAskData(obj);
		return rv;
	}
	
	@Override
	public String toString() {
		return this.getJsonString();
	}
}
